﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InsideDL.MISService;
using InsideDL.MISControl;
using InsideDL.MISData;

namespace InsideDL
{
    public partial class frmIG003 : MISControl.frmMatrix
    {
        EXLAPP exlAPP = null;

        public frmIG003()
        {
            InitializeComponent();
            Utility.LoadConfigValue("frmIG003");
            this.controlList = new List<ucSelFileOrPath>
            {
                ucBSFile,ucMYBSFile
            };
            ucExecute1.ExecuteClick += new EventHandler(ucClickFacade);
            ucExecute_Exit.ExecuteClick += new EventHandler(ucClickFacade);

        }

        protected override void execute()
        {
            if (base.Vaildation().Equals(false))                
            { return; }

            lstResult.Items.Clear();

            string BalanceSheet, Assets, Debt, AssetsSheet, DebtSheet,DebtsRow
                , AssetsRow;

            BalanceSheet = Utility.GetConfigValue("BalanceSheet");
            Assets = Utility.GetConfigValue("Assets");
            Debt = Utility.GetConfigValue("Debt");
            AssetsSheet = Utility.GetConfigValue("AssetsSheet");
            DebtSheet = Utility.GetConfigValue("DebtSheet");
            DebtsRow = Utility.GetConfigValue("DebtsRow");
            AssetsRow = Utility.GetConfigValue("AssetsRow");
            //SourceCol = Utility.GetConfigValue("SourceCol");

            //取得目前最新要填放的column
            string startYear = Utility.GetConfigValue("StartYear");
            string[] fl = System.IO.Path.GetFileName(ucBSFile.Txt).Split(new char[] { ' ' });
            char[] chs = Utility.GetConfigValue("SourceCol").ToCharArray();

            int newMonth ;
            //2012-03-20 要加月份，而不是加1
            newMonth = Convert.ToInt16(fl[0]) > Convert.ToInt16(startYear)
                ? (Convert.ToInt16(fl[0]) - Convert.ToInt16(startYear)) * 12 + Convert.ToInt16(fl[1])
                : Convert.ToInt16(fl[1]);
            
            //string nrng = Convert.ToChar(Convert.ToInt16(chs[0])
            //    + Convert.ToInt16(fl[1]) ).ToString();
            string nrng = Convert.ToChar(Convert.ToInt16(chs[0])
                + newMonth).ToString();

            //string xxx = Utility.GetNewRange(Utility.GetConfigValue("SourceCol")
            //    , "", ucBSFile.Txt, ' ');

            exlAPP = new EXLAPP();            
            exlAPP.OpenFile(ucMYBSFile.Txt);
            exlAPP.OpenFile(ucBSFile.Txt);
            //step1 先copy&paste Assets

            //object[,] aaa = exlAPP.GetSheetRangeArray(AssetsSheet
            //    , Utility.GetConfigValue("AssetsSheetCol"));
            string[] accArr1 =  exlAPP.GetSheetRangeArray(AssetsSheet
                , Utility.GetConfigValue("AssetsSheetCol"),true);
            string[] accArr2 = exlAPP.GetSheetRangeArray(BalanceSheet
                , Utility.GetConfigValue("BalanceSheet_Assets"), true);

            verify(accArr2, accArr1, AssetsSheet, AssetsRow);

            accArr1 = exlAPP.GetSheetRangeArray(DebtSheet
                , Utility.GetConfigValue("DebtSheetCol"), true);
            accArr2 = exlAPP.GetSheetRangeArray(BalanceSheet
                , Utility.GetConfigValue("BalanceSheet_DebtSheet"), true);

            verify(accArr2, accArr1, DebtSheet,DebtsRow);

            exlAPP.CopyPaste(BalanceSheet, Assets, AssetsSheet
                , string.Format("{0}{1}", nrng, AssetsRow),false);
            

            //step2 copy&paste Debt
            exlAPP.CopyPaste(BalanceSheet, Debt, DebtSheet
                 , string.Format("{0}{1}", nrng, DebtsRow,true));


            exlAPP.SaveCloseXLS();
            showMessage(this.Text, "完成！");
        }

        /// <summary>
        /// 以每月更新資料-資負及損益與業管.xls的兩個sheet
        /// 會計科目名稱來和YYY MM 自結資產負債表及損益表
        /// 比科目名有沒有不一樣
        /// 2002-04-19 新加入p_row 用來表示目前出錯的ROW
        /// </summary>
        protected bool verify(string[] p_source,string[] p_target,string p_sht
            ,string p_row)
        {
            /*
             * 1.取出目的檔(每月更新資料-資負及損益與業管)中的
             *   科目名
             * 2.再取出來源中的科目名稱
             * 3.比對名稱有沒有一樣
             * 4.如果有一樣回傳true，反之false
             * 
             */
            bool ok = true;
            string tmpT,tmpS;
            tmpT = string.Empty;
            tmpS = string.Empty;
            if (p_target.Length.Equals(p_source.Length))
            {
                for (int i = 0; i < p_target.Length; i++)
                {
                    tmpT = string.IsNullOrEmpty(p_target[i]) ? string.Empty
                        : p_target[i];
                    tmpS = string.IsNullOrEmpty(p_source[i]) ? string.Empty
                        : p_source[i];

                    if(!tmpT.Equals(tmpS))
                    {
                        ok = false;
                        writeErr(string.Format("{3}：ROW:{2}、{0}、{1}"
                        , p_target[i], p_source[i]
                        ,(Convert.ToInt16(p_row)+i).ToString()
                        , p_sht)                        
                        );

                    }
                }
            }
            else
            {
                ok = false;
                writeErr(string.Format("{2}：科目列數有錯target:{0}、source:{1}"
                    ,p_target.Length,p_source.Length, p_sht));
            }
            return ok;
        }

        protected void writeErr(string p)
        {
            lstResult.Items.Add(p);
        }
    }
}
